﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
namespace SIPO.Web.Forms.Transaction
{
    public partial class recRegistrationStatusUpdate : BasePage
    {
        private int id = -1;
        private int registrationId = -1;
        private string InvoiceNo = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            recRegistrationStatusRepo recRegistrationStatusDal = new recRegistrationStatusRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }
            if (Request.Params["registrationId"] != null)
            {
                registrationId = Convert.ToInt32(Request.Params["registrationId"]);
            }
            if (Request.Params["invoiceno"]!=null)
            {
               InvoiceNo = Request.Params["invoiceno"].ToString();
            }
            if (!Page.IsPostBack)
            {
                txtPaymentDate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
                try
                {
                    //txtPaymentValue.Text = recRegistrationStatusDal.GetInvoiceValue(InvoiceNo).ToString();
                }
                catch(Exception ex)
                {
                    throw (ex);
                }
                    if (id > 0) // Edit an existing item
                {
                    recSectorRegistrationRepo recSectorRegistrationDal = new recSectorRegistrationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recSectorRegistration recSectorRegistration = recSectorRegistrationDal.GetByIdForUpdateStatus(id);
                    if (recSectorRegistration != null)
                    {
                        hfID.Value = registrationId.ToString();
                        hfInvoiceNo.Value = recSectorRegistration.InvoiceNo;
                        hfSectorRegistrationID.Value = recSectorRegistration.SectorRegistrationID.ToString();
                        txtPaymentNo.Text = recSectorRegistration.PaymentNo;
                        if (recSectorRegistration.PaymentNo != "")
                            txtPaymentNo.Enabled = false;
                        if (recSectorRegistration.PaymentValue.ToString() != "")
                        {
                            if (recSectorRegistration.PaymentValue.ToString().Contains('.'))
                                txtPaymentValue.Text = recSectorRegistration.PaymentValue.ToString().Substring(0, recSectorRegistration.PaymentValue.ToString().IndexOf('.'));
                            else if (recSectorRegistration.PaymentValue.ToString().Contains(','))
                                txtPaymentValue.Text = recSectorRegistration.PaymentValue.ToString().Substring(0, recSectorRegistration.PaymentValue.ToString().IndexOf(','));
                            //txtPaymentValue.Text = recSectorRegistration.PaymentValue.ToString();
                        }
                        if (recSectorRegistration.PaymentValue.ToString() != "")
                            txtPaymentValue.Enabled = false;
                        if (recSectorRegistration.PaymentDate.ToString() != "")
                        {
                            txtPaymentDate.Text = recSectorRegistration.PaymentDate.Value.ToString("dd/MM/yyyy");
                            txtPaymentDate.Enabled = false;
                        }
                        List<recRegistrationStatus> registrationStatuss = recRegistrationStatusDal.GetList();
                        ddlRegistrationStatus.Items.Clear();
                        registrationStatuss.ForEach(delegate(recRegistrationStatus registrationStatus)
                        {
                            if (recSectorRegistration.recRegistrationStatus.RegistrationStatusName.ToLower().Contains("menunggu"))
                            {
                                if (registrationStatus.RegistrationStatusName.ToLower().Contains("sedang"))
                                    ddlRegistrationStatus.Items.Add(new ListItem(registrationStatus.RegistrationStatusName, registrationStatus.RegistrationStatusID.ToString()));
                            }
                            else if (recSectorRegistration.recRegistrationStatus.RegistrationStatusName.ToLower().Contains("sedang"))
                            {
                                if (registrationStatus.RegistrationStatusName.ToLower().Contains("terima") || registrationStatus.RegistrationStatusName.ToLower().Contains("tolak"))
                                    ddlRegistrationStatus.Items.Add(new ListItem(registrationStatus.RegistrationStatusName, registrationStatus.RegistrationStatusID.ToString()));
                            }
                            else if (recSectorRegistration.recRegistrationStatus.RegistrationStatusName.ToLower().Contains("terima"))
                            {
                                if (registrationStatus.RegistrationStatusName.ToLower().Contains("terima"))
                                    ddlRegistrationStatus.Items.Add(new ListItem(registrationStatus.RegistrationStatusName, registrationStatus.RegistrationStatusID.ToString()));
                            }
                            else if (recSectorRegistration.recRegistrationStatus.RegistrationStatusName.ToLower().Contains("tolak"))
                            {
                                if (registrationStatus.RegistrationStatusName.ToLower().Contains("tolak"))
                                    ddlRegistrationStatus.Items.Add(new ListItem(registrationStatus.RegistrationStatusName, registrationStatus.RegistrationStatusID.ToString()));
                            }
                            
                        });
                        ddlRegistrationStatus.Items.Insert(0, new ListItem("-- pilih status registrasi --", string.Empty));
                        //ddlRegistrationStatus.Items.FindByValue(recSectorRegistration.RegistrationStatusID.ToString()).Selected = true;
                        lblStatusRegistrasi.Text = recSectorRegistration.recRegistrationStatus.RegistrationStatusName;
                        if (recSectorRegistration.recRegistrationStatus.RegistrationStatusName.ToLower().Contains("terima") || recSectorRegistration.recRegistrationStatus.RegistrationStatusName.ToLower().Contains("tolak"))
                        {
                            ddlRegistrationStatus.Enabled = false;
                        }
                    }
                }
            }
            txtPaymentValue.Focus();
        }
    }
}